package com.yzx.lifeassistants.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebSettings.LayoutAlgorithm;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yzx.lifeassistants.R;
import com.yzx.lifeassistants.base.BaseActivity;
import com.yzx.lifeassistants.bean.JobInfo;
import com.yzx.lifeassistants.model.IQueryJobInfo;
import com.yzx.lifeassistants.model.callback.IQueryJobInfoCB;
import com.yzx.lifeassistants.model.impl.QueryJobInfoImpl;
import com.yzx.lifeassistants.utils.ToastUtils;
import com.yzx.lifeassistants.view.widget.CircularLoadingDialog;

/**
 * @Description: 招聘信息详情
 * @author: yzx
 * @time: 2016-3-20 上午11:13:03
 */
public class JobInfoActivity extends BaseActivity implements OnClickListener {

	private ImageButton backBtn;// 返回按钮
	private LinearLayout topLL;// 顶部内容
	private TextView titleTV;// 标题
	private TextView dateTV;// 时间
	private TextView placeTV;// 时间
	private WebView detailWV;// 详细内容

	private String url;// URL
	private CircularLoadingDialog dialog;// 加载框
	private Handler handler;//
	private IQueryJobInfoCB queryCallBack;// 回调
	private IQueryJobInfo queryJobInfo;// 查询
	private String html;//
	private WebViewClient canGoClient;// 能跳转网页

	/**
	 * @Description: Html页面头部
	 */
	public static final String JOB_INFO_HTML_HEAD = "<!DOCTYPE html>"
			+ "<head>"
			+ "<meta name='viewport' content='width=device-width, initial-scale=1.0, minimum-scale=0.8, maximum-scale=1.0, user-scalable=no' />"
			+ "<meta http-equiv='Content-Type' content='text/html; charset=gb2312' />"
			+ "</head>" + "<body>";

	/**
	 * @Description: Html页面尾部
	 */
	public static final String JOB_INFO_HTML_END = "</body>"
			+ "<script>  document.body.style.lineHeight = 1.5 </script>"
			+ "</html>";

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_job_info);
		init();
		initView();
		setListener();
		initData();
	}

	/**
	 * 
	 * @Description: 初始化
	 */
	private void init() {
		Intent intent = getIntent();
		if (null == intent) {
			url = "";
		} else {
			url = intent.getStringExtra("JOB_INFO_URL");
		}
	}

	/**
	 * 
	 * @Description: 初始化控件
	 */
	private void initView() {
		backBtn = (ImageButton) findViewById(R.id.job_info_back_btn);
		topLL = (LinearLayout) findViewById(R.id.job_info_top_ll);
		titleTV = (TextView) findViewById(R.id.job_info_title_tv);
		dateTV = (TextView) findViewById(R.id.job_info_date_tv);
		placeTV = (TextView) findViewById(R.id.job_info_place_tv);
		detailWV = (WebView) findViewById(R.id.job_info_detail_wv);
	}

	/**
	 * 
	 * @Description: 设置监听
	 */
	private void setListener() {
		backBtn.setOnClickListener(this);
	}

	/**
	 * 
	 * @Description: 初始化数据
	 */
	@SuppressLint({ "SetJavaScriptEnabled" })
	private void initData() {
		dialog = new CircularLoadingDialog(this);
		handler = new Handler();
		queryCallBack = new IQueryJobInfoCB() {

			@Override
			public void querySuccess(final JobInfo info) {
				handler.post(new Runnable() {

					@Override
					public void run() {
						titleTV.setText(info.getTitle());
						dateTV.setText(info.getTime());
						placeTV.setText(info.getPlace());
						html = JOB_INFO_HTML_HEAD + info.getContent()
								+ JOB_INFO_HTML_END;
						detailWV.loadDataWithBaseURL(null, html, "text/html",
								"utf-8", null);

					}
				});
			}

			@Override
			public void queryError(final int code) {
				handler.post(new Runnable() {

					@Override
					public void run() {
						dialog.dismiss();
						switch (code) {
						case IQueryJobInfoCB.RESULR_NULL: {
							showToast("获取失败，请确认网络连接后刷新重试~");
							break;
						}
						case IQueryJobInfoCB.QUERY_ERROR: {
							showToast("获取异常，请确认网络连接后刷新重试~");
							break;
						}
						default:
							break;
						}
					}
				});
			}
		};
		queryJobInfo = new QueryJobInfoImpl(this, queryCallBack);

		WebSettings webSettings = detailWV.getSettings();
		webSettings.setJavaScriptEnabled(true);// 支持JS
		detailWV.getSettings().setSupportZoom(true);// 支持屏幕缩放
		detailWV.getSettings().setBuiltInZoomControls(true);
		detailWV.getSettings().setDisplayZoomControls(false);// 不显示webview缩放按钮
		webSettings.setAllowContentAccess(true);
		webSettings.setAppCacheEnabled(false);
		webSettings.setBuiltInZoomControls(true);
		webSettings.setUseWideViewPort(true);
		webSettings.setLoadWithOverviewMode(true);
		webSettings.setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN);
		detailWV.setWebChromeClient(new WebChromeClient() {

			@Override
			public void onProgressChanged(WebView view, int newProgress) {

				if (newProgress == 100) {
					dialog.dismiss();
				}
				super.onProgressChanged(view, newProgress);
			}

		});
		canGoClient = new WebViewClient() {
			@Override
			public boolean shouldOverrideUrlLoading(WebView view, String url) {
				dialog.show();// 在WebView里实现页面跳转
				detailWV.setWebViewClient(null);
				topLL.setVisibility(View.GONE);// 防止在新的url里再跳转
				view.loadUrl(url);
				return true;
			}
		};
		detailWV.setWebViewClient(canGoClient);

		queryJobInfo();
	}

	/**
	 * 
	 * @Description: 获取通知详情
	 */
	private void queryJobInfo() {
		if (null == url || "".equals(url)) {
			showToast("获取异常，请确认网络连接后刷新重试~");
		} else {
			dialog.show();
			queryJobInfo.queryJobInfo(url);
		}
	}

	/**
	 * 
	 * @Description: 按键监听
	 */
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.job_info_back_btn: {// 返回
			if (detailWV.canGoBack()) {
				detailWV.goBack();
				dialog.show();
				topLL.setVisibility(View.VISIBLE);
				detailWV.setWebViewClient(canGoClient);// 返回后设置可跳转
				detailWV.loadDataWithBaseURL(null, html, "text/html", "utf-8",
						null);
				return;
			}
			finish();
			break;
		}

		default:
			break;
		}
	}

	/**
	 * 
	 * @Description: 重写返回键
	 */
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
			if (detailWV.canGoBack()) {
				detailWV.goBack();
				dialog.show();
				topLL.setVisibility(View.VISIBLE);
				detailWV.setWebViewClient(canGoClient);// 返回后设置可跳转
				detailWV.loadDataWithBaseURL(null, html, "text/html", "utf-8",
						null);
				return false;
			}
		}
		return super.onKeyDown(keyCode, event);
	}

	/**
	 * 
	 * @Description: Toast显示信息
	 */
	private void showToast(String message) {
		ToastUtils.showAlertToast(this, message, R.id.job_info_top_ll);

	}
}
